package com.bree.erp.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Transient;

@NamedQuery(name=ItemRequest.QUERY_SELECT_ITEMREQUESTS_OF_PURCHASEREQUISITION,
query="SELECT ir FROM ItemRequest ir WHERE ir.purchaseRequisition.id = :parentId")
@Entity
public class ItemRequest implements CrudId<Long>{
	
	public static final String QUERY_SELECT_ITEMREQUESTS_OF_PURCHASEREQUISITION = "selectItemRequestsOfPurchaseRequisition";

	@Id
	@GeneratedValue(strategy=GenerationType.SEQUENCE)
	private Long id;
	
	@ManyToOne
	private PurchaseRequisition purchaseRequisition;
	
	@OneToOne
	private PartUnit partUnit;
	
	private double quantity;
	
	private String remarks;
	
	@Transient
	private int itemNumber;
	
	@Override
	public int hashCode() {
		if(id != null) {
			return id.hashCode();
		}
		
		return itemNumber;
	}
	
	@Override 
	public boolean equals(Object obj) {
		
		if(obj instanceof ItemRequest) {
			ItemRequest that = (ItemRequest) obj;
			
			if(this.getId() != null && this.getId().equals(that.getId())) {
				return true;
			}
		}
		
		return false;
	}

	/**
	 * @return the id
	 */
	public Long getId() {
		
		if(id == null && itemNumber > 0) {
			return itemNumber * -1L;
		}
		
		
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return the purchaseRequisition
	 */
	public PurchaseRequisition getPurchaseRequisition() {
		return purchaseRequisition;
	}

	/**
	 * @param purchaseRequisition the purchaseRequisition to set
	 */
	public void setPurchaseRequisition(PurchaseRequisition purchaseRequisition) {
		this.purchaseRequisition = purchaseRequisition;
	}

	/**
	 * @return the quantity
	 */
	public double getQuantity() {
		return quantity;
	}

	/**
	 * @param quantity the quantity to set
	 */
	public void setQuantity(double quantity) {
		this.quantity = quantity;
	}

	/**
	 * @return the remarks
	 */
	public String getRemarks() {
		return remarks;
	}

	/**
	 * @param remarks the remarks to set
	 */
	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	/**
	 * @return the partUnit
	 */
	public PartUnit getPartUnit() {
		return partUnit;
	}

	/**
	 * @param partUnit the partUnit to set
	 */
	public void setPartUnit(PartUnit partUnit) {
		this.partUnit = partUnit;
	}

	/**
	 * @return the itemNumber
	 */
	public int getItemNumber() {
		return itemNumber;
	}

	/**
	 * @param itemNumber the itemNumber to set
	 */
	public void setItemNumber(int itemNumber) {
		this.itemNumber = itemNumber;
	}
}
